package com.hq.hotl.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hq.hotl.dao.CustomerDao;
import com.hq.hotl.pojo.Customer;
import com.hq.hotl.pojo.House;
import com.hq.hotl.pojo.PageData;
import com.hq.hotl.service.CutomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.*;
import org.apache.shiro.subject.Subject;

import java.util.HashMap;
import java.util.Map;
import java.util.List;


@Service
public class CustomerServiceImpl implements CutomerService {
    //定义默认页码
    final public static int DEFAULT_PAGENUMBER=1;
    final public static int DEFAULT_PAGE_SIZE=5;
    @Autowired
    private CustomerDao customerDao;

   //customer登录
     @Override
    public Map<String,Object> login(String username,String password){
         //调用customerDao的方法，传入前台传入的username参数，获取到的是一个map对象
         Map<String, Object> result = new HashMap<>();
         //获取主题,代表当前用户的对象
         Subject subject = SecurityUtils.getSubject();
         //判断当前用户是否已经登录过了,没有登录过才需要登录，登录过了就不用登录了，isAuthenticated判断是否认证过
         if (!subject.isAuthenticated()) {
             //没有认证过
             //待认证的用户名和密码创建一个安全令牌对象
             AuthenticationToken token = new UsernamePasswordToken(username, password);
             try {
                 //shiro框架检查令牌，执行登录
                 subject.login(token);
                 //未知账号异常
             } catch (UnknownAccountException e) {
                 //用户不存在
                 return result;
                 //密码不正确
             } catch (IncorrectCredentialsException e) {   //用户密码不存在
                 //用户名和密码不匹配
                 return result;
             } catch (AuthenticationException e) {
                 //其他的情况
                 e.printStackTrace();
                 return result;
             }
         }

         //已经认证过
         //从shiro提供session对象中获取已经认证成功的用户信息
         Object customer= subject.getSession().getAttribute("user");
         result.put("customer",customer);
         System.out.println(customer);
         return result;
     }


//注册customer
    @Override
    public int zhuce(Customer customer) {
        int zhuce = customerDao.zhuce(customer);
        //判断是否注册成功
        if (zhuce==0){
            //未注册成功
            return 0;
        }
        else {
            return 1;
        }
    }
    //修改一条customer信息
    @Override
    public Boolean updateOneCustomer(Customer customer) {
        int i = customerDao.updateOneCustomer(customer);
        //判断是否修改成功
        if (i==0){
            //修改失败
            return false;
        }
        return true;
    }

    //通过id查询一条用户信息
    @Override
    public Customer queryOneCustomer(int id) {
        Customer customer = customerDao.queryOneCusomer(id);
        //判断customer对象是否为空
        if (customer==null){
            //查询结果为空
            return null;
        }

        return customer;
    }

    //删除一条customer信息
    @Override
    public Boolean deleteOne(int id) {
        //调用dao层的删除方法来实现业务
        int i = customerDao.deleteOne(id);
        //判断是否删除成功
        if (i==0){
            //删除失败
            return false;
        }
        else{
            return true;
        }

    }

    //分页获取全部用户信息
    @Override
    public PageData getAllByPage(int currentPage, int pageSize) {
     //判断入参是否合理
        if (currentPage<DEFAULT_PAGENUMBER){
            currentPage=DEFAULT_PAGENUMBER;
        }
        if (pageSize<DEFAULT_PAGE_SIZE){
            pageSize=DEFAULT_PAGE_SIZE;
        }
        //利用pageHelper插件开始分页，拦截下一次mybatis操作
        PageHelper.startPage(currentPage,pageSize);
        //查询全部的customer信息，信息被pageHelp拦截做了改变
        List<Customer> customerList = customerDao.queryAll();
        //pageInfo对象已经计算好了分页相关的所有信息
        PageInfo<Customer> pageInfo = new PageInfo<>(customerList);
        //修改当前currentPage超出pageSize，改为总页数
        if (currentPage>pageInfo.getPages()){
            currentPage=pageInfo.getPages();
        }
       //利用pageInfo对象来封装我们自己定义的PageData
        PageData<Customer> pageData = new PageData<>();
        //设置总页数
        pageData.setTotalPages(pageInfo.getPages());
        //设置总记录数
        pageData.setTotalSize(pageInfo.getTotal());
        //判断是否有上一页
        if (pageInfo.isHasPreviousPage()){
            //设置上一页页码，当前页码没有在首页
            pageData.setPreviousPage(pageInfo.getPrePage());
        }else{
            //当前页在首页
            pageData.setPreviousPage(DEFAULT_PAGENUMBER);
        }
        //设置下一页页码
        //判断是否有下一页
        if (pageInfo.isHasNextPage()){
            //当前页没有在尾页
            pageData.setNextPage(pageInfo.getNextPage());
        }else {
            //当前页在尾页
            pageData.setNextPage(pageInfo.getPages());
        }
        //设置当前页码和每页记录数
        pageData.setCurrentPage(currentPage);
        pageData.setPageSize(pageSize);
        //将每页的记录传给Data
        pageData.setData(pageInfo.getList());

        return pageData;
    }

    //修改密码
    @Override
    public int updatePassword(Customer customer) {
        int i = customerDao.updatePassword(customer);
        if (i == 0){
            System.out.println("修改失败");
        }else{
            System.out.println("修改成功");
        }
        return i;
    }

    @Override
    public Customer queryOne(String username) {
        Customer customer = customerDao.queryOne(username);
        return customer;
    }


//customer登录
    /*@Override
    public int login(String usernmame, String password) {
        //调用dao层方法
        Customer customer = customerDao.login(usernmame, password);
        //判断查询到的结果
        if (customer==null){
            return 0;
        }
        return 1;
    }*/

   /* @Override
    public Map<String, Object> map(String username, String password) {
        return null;
    }*/

    @Override
    public List<House> orderHouse(String type) {
         List<House> result= customerDao.orderHouse(type);
        return result;
    }

    @Override
    public int updateOne(String name) {
        int i = customerDao.updateOne(name);
        return i;
    }

    @Override
    public int insertOne(int custpmer_id,int order_customer_id, double price) {
        int i = customerDao.insertOne(custpmer_id,order_customer_id,price);
        return i;
    }
}


